﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;
using System.Text;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model.Enum;
namespace HZ.Supervise.Web.Secondphase.ReportArea
{
    public partial class RankOfCity : BasePage
	{
		private string areaId = "";
        string sortSource = "''";

        public RankOfCity()
        {
            this.ModuleNo = Module.趋势走向.ToString();
            this.PageOperateType = OperateType.县区排名.ToString();
        }

		protected void Page_Load(object sender, EventArgs e)
		{
			areaId = CurrentAccount.GetUser().Org.Area.AreaID;

			if (!Page.IsPostBack)
			{
				HZ.Supervise.Model.User user = CurrentAccount.GetUser();

				this.MaxforMonth1.Banding();

				Databing();
			}
		}

		private void Databing()
		{

			StringBuilder sbCurrentMonth = new StringBuilder();

			sbCurrentMonth.AppendFormat("   AND daytime='{0}'", this.MaxforMonth1.Getvalue());

			StringBuilder sbLastMonth = new StringBuilder();

			string _oldYearmonth = this.MaxforMonth1.Getvalue().Insert(4, "-") + "-01";//根据本月拼接字符串，以便于能够转化成datetime类型，从而能够调用datetime类中求得上个月的方法

			sbLastMonth.AppendFormat("  AND daytime='{0}'", Convert.ToDateTime(_oldYearmonth).AddMonths(-1).ToString("yyyyMM"));
            //修改者:蒋峰(2011-06-23)
            //备  注:现在用不着
			//string sortSource = "1";

            if (this.dropMort.SelectedValue == "0" || this.dropMort.SelectedValue == "1" || this.dropMort.SelectedValue == "2")
            {

                sbCurrentMonth.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                sbLastMonth.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                sortSource = this.dropMort.SelectedValue;

            }

			string orderOfrank = "";

			string orderBy = "";

			if (this.ddlorderby.SelectedValue == "PurchaseAmount" || this.ddlorderby.SelectedValue == "DistributeAmount" || this.ddlorderby.SelectedValue == "WareHouseAmount")
			{

				orderOfrank = " sum(" + this.ddlorderby.SelectedValue + ")";

				orderBy = this.ddlorderby.SelectedValue + "total  " + this.dropOrder.SelectedValue;

			}
			else
			{

				if (this.ddlorderby.SelectedValue == "WareHouseRate")
				{
					orderOfrank = " (case when SUM(WarehouseAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount))>1 then 1 else  CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount)) end)";
				}
				else
				{

                    orderOfrank = " (case when SUM(DistributeAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount))>1 then 1 else CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount)) end)";

				}

				orderBy = this.ddlorderby.SelectedValue + " " + this.dropOrder.SelectedValue;

			}

			IBLL_Area entity = BLLFactory.IBLL_Area();

			DataTable dt = new DataTable();

			int totalrecord = 0;

			if (CurrentAccount.GetUser().UserID.Contains("root"))
			{

				string strGroupBy = " substring(areaId,0,5)";

				dt = entity.GetProcureAreaMax2(strGroupBy, orderOfrank, orderBy, sbCurrentMonth.ToString(), sbLastMonth.ToString(), sortSource, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);

				HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "addDistributeRate");

				HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate", "addWareHouseRate");

				Function.AddColRank(dt, "pms");

				Function.AddColumn(dt, "SortSorce");

				this.AspNetPager1.RecordCount = totalrecord;

			}
			else
			{

				dt = null;

			}

			this.gvwProcureCatalog.DataSource = dt;

			this.gvwProcureCatalog.DataBind();

		}

		protected void btnQuery_Click(object sender, EventArgs e)
		{

			this.AspNetPager1.CurrentPageIndex = 1;

		}

		protected void btnExcel_Click(object sender, EventArgs e)
		{
			StringBuilder sbCurrentMonth = new StringBuilder();

			sbCurrentMonth.AppendFormat("   AND daytime='{0}'", this.MaxforMonth1.Getvalue());

			StringBuilder sbLastMonth = new StringBuilder();

			string _oldYearmonth = this.MaxforMonth1.Getvalue().Insert(4, "-") + "-01";//根据本月拼接字符串，以便于能够转化成datetime类型，从而能够调用datetime类中求得上个月的方法

			sbLastMonth.AppendFormat("  AND daytime='{0}'", Convert.ToDateTime(_oldYearmonth).AddMonths(-1).ToString("yyyyMM"));
            //修改者:蒋峰(2011-06-23)
            //备  注:现在用不着
			string sortSource = "1";

            if (this.dropMort.SelectedValue == "0" || this.dropMort.SelectedValue == "1" || this.dropMort.SelectedValue == "2")
            {

                sbCurrentMonth.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                sbLastMonth.AppendFormat(" and SortSorce = {0}", this.dropMort.SelectedValue);

                sortSource = this.dropMort.SelectedValue;

            }

			string orderOfrank = "";

			string orderBy = "";

			if (this.ddlorderby.SelectedValue == "PurchaseAmount" || this.ddlorderby.SelectedValue == "DistributeAmount" || this.ddlorderby.SelectedValue == "WareHouseAmount")
			{

				orderOfrank = " sum(" + this.ddlorderby.SelectedValue + ")";

				orderBy = this.ddlorderby.SelectedValue + "total  " + this.dropOrder.SelectedValue;

			}
			else
			{

				if (this.ddlorderby.SelectedValue == "WareHouseRate")
				{
                    orderOfrank = " (case when SUM(WarehouseAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount))>1 then 1 else  CONVERT(decimal(18,3),CONVERT(float, SUM(WarehouseAmount)) / SUM(PurchaseAmount)) end)";
				}
				else
				{

                    orderOfrank = " (case when SUM(DistributeAmount) = 0 then 0 when SUM(PurchaseAmount)=0 then 0 when CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount))>1 then 1 else CONVERT(decimal(18,3),CONVERT(float, SUM(DistributeAmount)) / SUM(PurchaseAmount)) end)";

				}

				orderBy = this.ddlorderby.SelectedValue + " " + this.dropOrder.SelectedValue;

			}

			IBLL_Area entity = BLLFactory.IBLL_Area();

			DataTable dt = new DataTable();

			int totalrecord = 0;

			if (CurrentAccount.GetUser().UserID.Contains("root"))
			{

				string strGroupBy = " substring(areaId,0,5)";

				dt = entity.GetProcureAreaMax2(strGroupBy, orderOfrank, orderBy, sbCurrentMonth.ToString(), sbLastMonth.ToString(), sortSource, 1, 0, out totalrecord);

				HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "addDistributeRate");

				HZ.Supervise.Web.Function.ChangeRate(dt, "WareHouseRate", "addWareHouseRate");

				Function.AddColRank(dt, "pms");

				Function.AddColumn(dt, "SortSorce");

				HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwProcureCatalog);
			}
			else
			{

				dt = null;

			}
		}

		protected void gvwProcureCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				Label lab = (Label)e.Row.FindControl("labid");
				if (lab != null)
				{
					if (lab.Text.Trim() == "2147483647")
					{
						lab.Text = "上月无排名";
					}
				}

				e.Row.Cells[0].Text = HZ.Supervise.Web.Function.GetCityNameByCountryName(e.Row.Cells[0].Text);
			}
		}

		protected void AspNetPager1_PageChanged(Object sender, EventArgs e)
		{

			this.Databing();

		}
	}
}
